

$(document).ready(
    function() {
        $("form").submit(function() {
            return false;
        });
        var VAL = {
            NORMAL : "0000000",
            EVT_USER : $("#userId").text(),
            T_FIPINQBOX: "FIPINQBOX",
            T_FIPINQSHT: "FIPINQSHT",
            T_FSPPRDSCRP : "FSPPRDSCRP",
            T_MIPWOBOM: "MIPWOBOM",
            T_FBPLSTDAT : "FBPBISDAT",
            DISABLED_ATTR : {
                "disabled" : true
            },
            ENABLED_ATTR : {
                "disabled" : false
            }
        };
        var domObj = {
            $window : $(window),

            $prdSeqId : $("#prdSeqId"),
            $boxId: $("#boxId"),
            $boxIdI : $("#boxIdI"),
            $shtOpeMsg : $("#shtOpeMsg"),
            $prdSeqRadio : $("#prdSeqRadio"),
            $boxIdRadio : $("#boxIdRadio"),

            $scrpMtrlProcId : $("#scrpMtrlProcId"),
            $scapDestId      : $("#scapDestId"),

            grids : {
                $prdInfoGrd : $("#prdInfoGrd"),
                $rsnInfoGrd : $("#rsnInfoGrd")
            },

            buttons : {
                $scrapped_btn : $("#scrapped_btn"),
                $cancel_btn : $("#cancel_btn"),
                $clean_btn : $("#clean_btn"),
                $addRsn_btn : $("#addRsn_btn"),
                $delRsn_btn : $("#delRsn_btn"),
                $delete_btn  : $("#delete_btn")
            },

            dialogs : {
                $addReasonDialog : $("#addReasonDialog"),
                $deptId : $("#deptId"),
                $modalReasonListDiv : $("#modalReasonListDiv"),
                $modalReasonListGrd : $("#modalReasonListGrd"),
                $modalReasonListPg : $("#modalReasonListPg"),

                $reModalReasonListDiv : $("#reModalReasonListDiv"),
                $reModalReasonListGrd : $("#reModalReasonListGrd"),
                $reModalReasonListPg : $("#reModalReasonListPg"),

                // 对话框的确认按钮
                $reasonDialog_sureBtn : $("#reasonDialog_sureBtn"),
            }

        };

        function initPrdInfoGrd() {
            domObj.grids.$prdInfoGrd.jqGrid({
                url : "",
                datatype : "local",
                mtype : "POST",
                height : 400,
                width : $("#prdInfoGrdDiv").width() * 0.99,
                autowidth: false,
                shrinkToFit:false,
                autoScroll: false,
                resizable : true,
                loadonce : true,
                viewrecords : true,
                rownumbers : true,
                rownumWidth : 30,
                rowNum : 20,
                emptyrecords : true,
                pager : "prdInfoPg",
                multiselect : true,
                colModel : [ {
                    name : 'prd_seq_id',
                    index : 'prd_seq_id',
                    label : '屏体代码',
                    width : 180
                }, {
                    name : 'wo_id_fk',
                    index : 'wo_id_fk',
                    label : '工单',
                    width : 180
                }, {
                    name : 'mdl_id_fk',
                    index : 'mdl_id_fk',
                    label : '产品名称',
                    width : 180
                }, {
                    name : 'prd_stat',
                    index : 'prd_stat',
                    label : '屏体状态',
                    width : 180
                }, {
                    name : 'pv_prd_stat_fk',
                    index : 'pv_prd_stat_fk',
                    label : '屏体上一状态',
                    width : 180
                }, {
                    name : 'scrp_stat',
                    index : 'scrp_stat',
                    label : '报废状态',
                    width : 180
                }, {
                    name  : 'cr_ope_id_fk',
                    index : 'cr_ope_id_fk',
                    label : '当前站点',
                    width : 180
                },{
                    name : 'ope_dsc',
                    index : 'ope_dsc',
                    label : '当前站点描述',
                    width : 180
                },{
                    name : 'sht_ope_msg',
                    index : 'sht_ope_msg',
                    label : '备注信息',
                    width : 280
                },{
                    name : 'evt_usr',
                    index : 'evt_usr',
                    label : '操作用户',
                    width : 180
                },{
                    name : 'evt_timestamp',
                    index : 'evt_timestamp',
                    label : '操作时间',
                    width : 180
                },],
            });
        }


        function initRsnInfoGrd(){
            domObj.grids.$rsnInfoGrd.jqGrid({
                url : "",
                datatype : "local",
                mtype : "POST",
                height : 150,
                width : $("#rsnInfoGrdDiv").width(),
                autowidth : true,
                shrinkToFit:false,
                autoScroll: false,
                resizable : true,
                loadonce : true,
                viewrecords : true,
                rownumbers : true,
                rowNum : 20,
                emptyrecords : true,
                multiselect : false,
                pager :  $("#rsnInfoPg"),
                colModel : [ {
                    name : 'dept_id',
                    index : 'dept_id',
                    label : '部门代码',
                    width : 120
                }, {
                    name : 'data_ext',
                    index : 'data_ext',
                    label : '原因代码',
                    width : 170
                }, {
                    name : 'data_desc',
                    index : 'data_desc',
                    label : '原因代码说明',
                    width : 200
                }]
            });
        };

        var inModelGrid = function() {
            domObj.dialogs.$modalReasonListGrd
                .jqGrid("clearGridData");
            domObj.dialogs.$reModalReasonListGrd
                .jqGrid("clearGridData");
            domObj.dialogs.$modalReasonListGrd
                .jqGrid({
                    url : "",
                    datatype : "local",
                    mtype : "POST",
                    height : 300,
                    // width :
                    // domObj.dialogs.$modalReasonListDiv.width()*0.99,
                    width : 400,
                    autowidth : false,
                    shrinkToFit : false,
                    resizable : true,
                    loadonce : true,
                    viewrecords : true,
                    rownumbers : true,
                    rowNum : 20,
                    emptyrecords : true,
                    multiselect : false,
                    pager : domObj.dialogs.$modalReasonListPg,
                    colModel : [ {
                        name : 'data_ext',
                        index : 'data_ext',
                        label : '原因代码',
                        width : 150
                    }, {
                        name : 'data_desc',
                        index : 'data_desc',
                        label : '原因代码说明',
                        width : 180
                    } ],
                    onSelectRow : function(rowId, status) {
                        var dept_id = domObj.dialogs.$deptId.val();
                        if(!dept_id){
                            showErrorDialog("", "请先选择部门代码！");
                            return false;
                        }

                        var rowId= domObj.dialogs.$modalReasonListGrd
                            .jqGrid("getGridParam", "selrow");
                        var rowData = domObj.dialogs.$modalReasonListGrd.jqGrid(
                            "getRowData", rowId);
                        var rsn_data={
                            dept_id  : dept_id,
                            data_ext : rowData.data_ext,
                            data_desc  : rowData.data_desc
                        }
                        setGridInfo(rsn_data, "#reModalReasonListGrd");
                    }
                });

            domObj.dialogs.$reModalReasonListGrd.jqGrid({
                url : "",
                datatype : "local",
                mtype : "POST",
                height : 400,
                // width:domObj.dialogs.$reModalReasonListDiv.width()*
                // 0.99,
                width : 400,
                autowidth : false,
                shrinkToFit : false,
                resizable : true,
                loadonce : true,
                viewrecords : true,
                rownumbers : true,
                rowNum : 20,
                emptyrecords : true,
                multiselect  : false,
                pager : domObj.dialogs.$reModalReasonListPg,
                colModel : [ {
                    name : 'dept_id',
                    index : 'dept_id',
                    label : '部门代码',
                    width : 100
                }, {
                    name : 'data_ext',
                    index : 'data_ext',
                    label : '原因代码',
                    width : 100
                }, {
                    name : 'data_desc',
                    index : 'data_desc',
                    label : '原因代码说明',
                    width : 100
                } ]
            });
        };

        var dialogFnc = {
            reasonSureFnc : function() {
                var Data = domObj.dialogs.$reModalReasonListGrd.jqGrid("getRowData");
                if(Data.length<1){
                    showErrorDialog("", "请选择一条原因代码");
                    return false;
                }
                setGridInfo(Data, "#rsnInfoGrd");
                domObj.dialogs.$addReasonDialog.modal("hide");
            }
        }

        //radio
        domObj.$prdSeqRadio.click(function(){
            //alert(this.checked);
            var $radio = $(this);
            // if this was previously checked
            $radio.prop('checked', true);
            domObj.$boxIdRadio.prop('checked', false);
            domObj.$boxId.attr(VAL.DISABLED_ATTR);
            domObj.$prdSeqId.attr(VAL.ENABLED_ATTR);
            domObj.$prdSeqId.val("");
            domObj.$prdSeqId.focus();
            domObj.$boxId.val("");
        });

        domObj.$boxIdRadio.click(function(){
            var $radio = $(this);
            $radio.prop('checked', true);
            domObj.$prdSeqRadio.prop('checked', false);
            domObj.$boxId.attr(VAL.ENABLED_ATTR);
            domObj.$prdSeqId.attr(VAL.DISABLED_ATTR);
            domObj.$prdSeqId.val("");
            domObj.$boxId.val("");
            domObj.$boxId.focus();
        });
        var toolFnc = {

            initMtrlProc : function (woIdFk) {
                var inObj = {
                    trx_id : VAL.T_MIPWOBOM,
                    wo_id_fk :woIdFk,
                    action_flg : "R",
                    mtrl_type :"X"
                };
                var outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code == VAL.NORMAL) {
                    SelectDom.addSelectArr(domObj.$scrpMtrlProcId, outObj.oaryA, "mtrl_prod_id_fk","mtrl_prod_id_fk","",true);
                }
            },
            initDestShop : function () {
                var inObj = {
                    trx_id : VAL.T_FBPLSTDAT,
                    action_flg : "Q",
                    iary : [{
                        data_cate: 'DEST',
                        data_item  : 'L'
                    }]
                };
                var outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code == VAL.NORMAL) {
                    SelectDom.addSelectArr(domObj.$scapDestId, outObj.oary, "data_ext","data_desc","",true);
                }
            },
        }
        var buttonFnc = {
            scrappedFnc : function (){
                var prdSeqId,boxId,shtOpeMsg,rsnId,iary,rowData,inObj,outObj;
                var iary = new Array();
                var scrpMtrl = $.trim(domObj.$scrpMtrlProcId.val());
                var scapDest = $.trim(domObj.$scapDestId.val());
                shtOpeMsg = domObj.$shtOpeMsg.val();
                var rowDatas = $("#prdInfoGrd").jqGrid("getRowData");
                if(rowDatas.length ==0){
                    showErrorDialog("", "请选择需要报废的玻璃！");
                    return false;
                }
                for(var i=0;i<rowDatas.length;i++){
                    var prd_id={
                        prd_seq_id : rowDatas[i].prd_seq_id
                    }
                    iary.push(prd_id);
                    if(rowDatas[i].prd_stat.trim() == "SCRP"){
                        showErrorDialog("", "玻璃["+rowDatas[i].prd_seq_id+"]已经报废过了，无需再次报废");
                        return false;
                    }
                }
                if(!scrpMtrl){
                    showErrorDialog("", "请选择报废料号！");
                    return false;
                }
                if(!scapDest){
                    showErrorDialog("", "请选择报废仓库！");
                    return false;
                }
                var rowIds = domObj.grids.$rsnInfoGrd.jqGrid("getDataIDs");
                if(rowIds.length == 0){
                    showErrorDialog("", "请选择报废部门及原因代码！");
                    return false;
                }

                if(!shtOpeMsg){
                    showErrorDialog("", "备注信息不能为空！");
                    return false;
                }

                rowData = domObj.grids.$rsnInfoGrd.jqGrid("getRowData",rowIds[0]);

                var rsn_dept = rowData.dept_id;
                var rsn_code = rowData.data_ext;
                var dataDesc = rowData.data_desc;
                inObj = {
                    trx_id : VAL.T_FSPPRDSCRP,
                    iary : iary,
                    dest_shop:scapDest,
                    mtrl_prod_id : scrpMtrl,
                    sht_ope_msg : shtOpeMsg,
                    rsn_dept : rsn_dept,
                    rsn_cate : "SCRS",
                    rsn_code : rsn_code,
                    evt_usr : VAL.EVT_USER,
                    action_flg : "S"
                };
                outObj = comTrxSubSendPostJson(inObj);
                if(outObj.rtn_code === VAL.NORMAL){
                    domObj.grids.$prdInfoGrd.jqGrid('clearGridData');
                    showSuccessDialog("报废成功！");
                }
            },

            cancelFnc  : function() {
                var shtOpeMsg ,id, inObj, outObj,rowData,iary;
                shtOpeMsg = domObj.$shtOpeMsg.val();
                var iary = new Array();
                var rowDatas = $("#prdInfoGrd").jqGrid("getRowData");
                if(rowDatas.length ==0){
                    showErrorDialog("", "请选择需要取消报废的玻璃！");
                    return false;
                }
                for(var i=0;i<rowDatas.length;i++){
                    if(rowDatas[i].prd_stat.trim() != "SCRP"){
                        showErrorDialog("", "玻璃["+rowDatas[i].prd_seq_id+"]未报废，无需取消报废！");
                        return false;
                    }
                    var prd_id={
                        prd_seq_id : rowDatas[i].prd_seq_id
                    }
                    iary.push(prd_id);
                }

                var pvPrdStatFk = rowDatas[0].pv_prd_stat_fk;
                var boxIdI = domObj.$boxIdI.val().trim();
                if(!shtOpeMsg){
                    showErrorDialog("", "备注信息不能为空！");
                    return false;
                }

                inObj = {
                    trx_id : VAL.T_FSPPRDSCRP,
                    action_flg : 'C',
                    iary : iary,
                    box_id_cancel_in :  boxIdI,
                    sht_ope_msg : shtOpeMsg,
                    evt_usr : VAL.EVT_USER
                };
                outObj = comTrxSubSendPostJson(inObj);
                if(outObj.rtn_code === VAL.NORMAL){
                    showSuccessDialog("取消报废成功！");
                }
            },

            cleanFnc : function() {
                domObj.$boxId.val("");
                domObj.$boxIdI.val("");
                domObj.$prdSeqId.val("");
                domObj.$shtOpeMsg.val("");
                domObj.$prdSeqRadio.prop('checked',false);
                domObj.$boxIdRadio.prop('checked',false);
                //domObj.$prdSeqId.attr(VAL.DISABLED_ATTR);
                domObj.$boxId.attr(VAL.DISABLED_ATTR);
                domObj.grids.$prdInfoGrd.jqGrid('clearGridData');
                domObj.grids.$rsnInfoGrd.jqGrid('clearGridData');
                $("#modalReasonListDiv").jqGrid('clearGridData');
                $("#reModalReasonListDiv").jqGrid('clearGridData');

            },

            selectReasonBtnfunc : function() {
                var rowCnt = domObj.grids.$prdInfoGrd.getGridParam("reccount");
                if(rowCnt == 0){
                    showErrorDialog("", "请先获取需要报废的玻璃信息，再选择原因代码！");
                    return false;
                }

                domObj.dialogs.$modalReasonListGrd
                    .jqGrid("clearGridData");
                domObj.dialogs.$reModalReasonListGrd
                    .jqGrid("clearGridData");
                // 原因代码对话框的模型
                domObj.dialogs.$addReasonDialog.modal({
                    backdrop : true,
                    keyboard : true,
                    show : false
                });
                //初始化部门代码
                comAddValueByDataCateFnc("#deptId","DEPT","data_ext","data_desc",true);

                //初始化报废原因代码
                var iary, inObj, outObj;
                deptId = domObj.dialogs.$deptId.val();
                iary = {
                    data_cate : "SCRS"
                };
                inObj = {
                    trx_id : VAL.T_FBPLSTDAT,
                    action_flg : "Q",
                    iary : [ iary ]
                };
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
                    var deptIds = domObj.dialogs.$deptId.val();
                    setGridInfo(outObj.oary,"#modalReasonListGrd");
                    domObj.dialogs.$addReasonDialog.modal("show");
                }
            },
            deleteReasonBtnfunc : function() {
                var selRowId = domObj.grids.$rsnInfoGrd.jqGrid(
                    "getGridParam", "selrow");
                if (selRowId==null||selRowId.length < 1) {
                    showErrorDialog("", "请至少选择一条原因代码进行删除");
                    return false;
                }
                $("#deleteReason_btn").showCallBackWarnningDialog(
                    {
                        errMsg : "是否删选原因代码,请确认!!!!",
                        callbackFn : function(data) {
                            if (data.result == true) {
                                // 动态删除jqgrid行时，数据会在动态变化
                                var length = selRowId.length;
                                for (var i = 0; i < length; i++) {
                                    domObj.grids.$rsnInfoGrd.jqGrid("delRowData",selRowId[0]);
                                }
                            }
                        }
                    });
            }

        };

        domObj.buttons.$scrapped_btn.bind('click', buttonFnc.scrappedFnc);
        domObj.buttons.$cancel_btn.bind('click', buttonFnc.cancelFnc);
        domObj.buttons.$clean_btn.bind('click', buttonFnc.cleanFnc);
        domObj.buttons.$addRsn_btn.bind('click',buttonFnc.selectReasonBtnfunc);
        domObj.buttons.$delRsn_btn.bind('click',buttonFnc.deleteReasonBtnfunc);
        domObj.dialogs.$reasonDialog_sureBtn.bind('click',dialogFnc.reasonSureFnc);

        domObj.$boxId.keydown(function(event) {
            var inObj, outObj, tblCnt, boxId, newRowId, iary;
            if (event.keyCode == 13) {
                boxId = domObj.$boxId.val();
                domObj.$boxIdI.val("");
                inObj = {
                    trx_id : VAL.T_FIPINQBOX,
                    action_flg : 'Q',
                    box_id : boxId
                },
                    outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
                    if(outObj.box_stat.trim() != "WAIT"){
                        showErrorDialog("", "整卡匣报废时，箱子的状态必须是[WAIT]！");
                        return false;
                    }

                    domObj.$shtOpeMsg.val("");
                    if(outObj.oary[0].prd_stat.trim() != "WAIT"){
                        showErrorDialog("", "整卡匣报废时，必须是[WAIT]状态");
                        return false;
                    }
                    setGridInfo(outObj.oary, "#prdInfoGrd", true);
                }
            }
        });

        domObj.$prdSeqId.keydown(function(event) {
            var inObj, outObj, tblCnt, prdSeqId, newRowId, iary;
            if (event.keyCode == 13) {
                prdSeqId = domObj.$prdSeqId.val();
                inObj = {
                    trx_id : VAL.T_FIPINQSHT,
                    action_flg : 'Q',
                    prd_seq_id : prdSeqId
                },
                    outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
                    if(outObj.prd_stat != "SCRP" && outObj.prd_stat != "WAIT"){
                        showErrorDialog("", "单片报废时，必须是[WAIT]状态;取消报废时，状态必须是[SCRP]");
                        return false;
                    }
                    var rowDatas = $("#prdInfoGrd").jqGrid("getRowData");
                    if(rowDatas.length>0){
                        for (var i = 0; i < rowDatas.length; i++) {
                            if ($.trim(rowDatas[i].prd_seq_id) ==$.trim(prdSeqId) ) {
                                domObj.$prdSeqId.val("");
                                return;
                            }
                            if ($.trim(rowDatas[i].wo_id_fk) != outObj.wo_id_fk) {
                                showErrorDialog("", "该屏体工单["+rowDatas[i].wo_id_fk+"]与列表中工单不一致！");
                                return;
                            }
                            if ($.trim(rowDatas[i].cr_ope_id_fk) != outObj.cr_ope_id_fk) {
                                showErrorDialog("", "该屏体当前站点["+rowDatas[i].cr_ope_id_fk+"]与列表中工单当前站点不一致！");
                                return;
                            }
                        }
                    }
                    var newRowID = getGridNewRowID("#prdInfoGrd");
                    $("#prdInfoGrd").jqGrid("addRowData",newRowID,outObj);
                    //初始化半成品料号
                    toolFnc.initMtrlProc(outObj.wo_id_fk);
                    domObj.$prdSeqId.val("");
                    domObj.$prdSeqId.focus();
                }
            }
        });
        domObj.buttons.$delete_btn.click(function () {
            var rowid = domObj.grids.$prdInfoGrd.jqGrid('getGridParam','selrow');
            if(rowid == null){
                showErrorDialog("", "请选择需要删除的玻璃代码！");
                return false;
            }
            domObj.grids.$prdInfoGrd.jqGrid('delRowData', rowid);
        });
        function initPage(){
            domObj.$boxId.val("");
            domObj.$boxIdI.val("");
            domObj.$prdSeqId.val("");
            domObj.$shtOpeMsg.val("");
            domObj.$boxId.attr(VAL.DISABLED_ATTR);

            domObj.grids.$prdInfoGrd.jqGrid('clearGridData');
            $("#modalReasonListDiv").jqGrid('clearGridData');
            $("#reModalReasonListDiv").jqGrid('clearGridData');
            initPrdInfoGrd();
            initRsnInfoGrd();
            inModelGrid();
            toolFnc.initDestShop();
        };

        initPage();

        //表格自适应
        function resizeFnc(){
            var offsetBottom, divWidth;
            divWidth = $("#prdInfoGrdDiv").width();
            offsetBottom =  $(window).height() - $("#prdInfoGrdDiv").offset().top;
            $("#prdInfoGrdDiv").height(offsetBottom * 0.95);
            $("#prdInfoGrd").setGridWidth(divWidth * 0.99);
            $("#prdInfoGrd").setGridHeight(offsetBottom * 0.99 - 51);

            divWidth = $("#rsnInfoGrdDiv").width();
            offsetBottom = $("#rsnInfoGrdDiv").offset().top;
            $("#rsnInfoGrdDiv").height(offsetBottom * 0.95);
            $("#rsnInfoGrd").setGridWidth(divWidth * 0.99);
            $("#rsnInfoGrd").setGridHeight(offsetBottom * 0.99);
        };
        resizeFnc();
        $(window).resize(function() {
            resizeFnc();
        });
    })